<!DOCTYPE html><html class="default" lang="en"><head><meta charSet="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>network | @shumai/shumai</title><meta name="description" content="Documentation for @shumai/shumai"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script async src="../assets/search.js" id="search-script"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex/dist/katex.min.css"/><script src="https://cdn.jsdelivr.net/npm/katex/dist/katex.min.js"></script><script src="https://cdn.jsdelivr.net/npm/katex/dist/contrib/auto-render.min.js"></script><script>
					window.__typeDocPluginKatexOptions=undefined;
					window.addEventListener('load', () => window.renderMathInElement(document.body, window.__typeDocPluginKatexOptions));
				</script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os"</script><header class="tsd-page-toolbar">
<div class="tsd-toolbar-contents container">
<div class="table-cell" id="tsd-search" data-base="..">
<div class="field"><label for="tsd-search-field" class="tsd-widget tsd-toolbar-icon search no-caption"><svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M15.7824 13.833L12.6666 10.7177C12.5259 10.5771 12.3353 10.499 12.1353 10.499H11.6259C12.4884 9.39596 13.001 8.00859 13.001 6.49937C13.001 2.90909 10.0914 0 6.50048 0C2.90959 0 0 2.90909 0 6.49937C0 10.0896 2.90959 12.9987 6.50048 12.9987C8.00996 12.9987 9.39756 12.4863 10.5008 11.6239V12.1332C10.5008 12.3332 10.5789 12.5238 10.7195 12.6644L13.8354 15.7797C14.1292 16.0734 14.6042 16.0734 14.8948 15.7797L15.7793 14.8954C16.0731 14.6017 16.0731 14.1267 15.7824 13.833ZM6.50048 10.499C4.29094 10.499 2.50018 8.71165 2.50018 6.49937C2.50018 4.29021 4.28781 2.49976 6.50048 2.49976C8.71001 2.49976 10.5008 4.28708 10.5008 6.49937C10.5008 8.70852 8.71314 10.499 6.50048 10.499Z" fill="var(--color-text)"></path></svg></label><input type="text" id="tsd-search-field" aria-label="Search"/></div>
<div class="field">
<div id="tsd-toolbar-links"></div></div>
<ul class="results">
<li class="state loading">Preparing search index...</li>
<li class="state failure">The search index is not available</li></ul><a href="" class="title">@shumai/shumai</a></div>
<div class="table-cell" id="tsd-widgets"><a href="#" class="tsd-widget tsd-toolbar-icon menu no-caption" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none"><rect x="1" y="3" width="14" height="2" fill="var(--color-text)"></rect><rect x="1" y="7" width="14" height="2" fill="var(--color-text)"></rect><rect x="1" y="11" width="14" height="2" fill="var(--color-text)"></rect></svg></a></div></div></header>
<div class="container container-main">
<div class="col-8 col-content">
<div class="tsd-page-title">
<ul class="tsd-breadcrumb">
<li><a href="../modules.html">@shumai/shumai</a></li>
<li><a href="network.html">network</a></li></ul>
<h1>Namespace network</h1></div>
<section class="tsd-panel tsd-comment">
<div class="tsd-comment tsd-typography"><p>Below are some examples of how to use functions in the <code>network</code> namespace.</p>

<a href="#inference" id="inference" style="color: inherit; text-decoration: none;">
  <h3>Inference</h3>
</a>
<p>Want to expose your model to the network? Use <a href="../functions/network.serve.html"><code>network.serve</code></a>:</p>
<pre><code class="language-javascript"><span class="hl-2">import</span><span class="hl-0"> </span><span class="hl-3">*</span><span class="hl-0"> </span><span class="hl-2">as</span><span class="hl-0"> </span><span class="hl-4">sm</span><span class="hl-0"> </span><span class="hl-2">from</span><span class="hl-0"> </span><span class="hl-5">&#39;@shumai/shumai&#39;</span><br/><span class="hl-2">import</span><span class="hl-0"> { </span><span class="hl-4">model</span><span class="hl-0"> } </span><span class="hl-2">from</span><span class="hl-0"> </span><span class="hl-5">&#39;./model&#39;</span><br/><br/><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">network</span><span class="hl-0">.</span><span class="hl-7">serve</span><span class="hl-0">({</span><br/><span class="hl-0">  </span><span class="hl-7">run_model</span><span class="hl-4">:</span><span class="hl-0"> (</span><span class="hl-4">_</span><span class="hl-0">, </span><span class="hl-4">input</span><span class="hl-0">) </span><span class="hl-3">=&gt;</span><span class="hl-0"> {</span><br/><span class="hl-0">    </span><span class="hl-1">// output tensors are automatically serialized</span><br/><span class="hl-0">    </span><span class="hl-2">return</span><span class="hl-0"> </span><span class="hl-7">model</span><span class="hl-0">(</span><span class="hl-4">input</span><span class="hl-0">)</span><br/><span class="hl-0">  }</span><br/><span class="hl-0">})</span>
</code></pre>
<p>A client can use <a href="../functions/network.tfetch.html"><code>network.tfetch</code></a> (basically <code>fetch</code> but for Tensors):</p>
<pre><code class="language-javascript"><span class="hl-2">import</span><span class="hl-0"> </span><span class="hl-3">*</span><span class="hl-0"> </span><span class="hl-2">as</span><span class="hl-0"> </span><span class="hl-4">sm</span><span class="hl-0"> </span><span class="hl-2">from</span><span class="hl-0"> </span><span class="hl-5">&#39;@shumai/shumai&#39;</span><br/><br/><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-6">input</span><span class="hl-0"> = </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-7">randn</span><span class="hl-0">([</span><span class="hl-8">128</span><span class="hl-0">])</span><br/><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-6">url</span><span class="hl-0"> = </span><span class="hl-5">&#39;localhost:3000/run_model&#39;</span><br/><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-6">output</span><span class="hl-0"> = </span><span class="hl-2">await</span><span class="hl-0"> </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">network</span><span class="hl-0">.</span><span class="hl-7">tfetch</span><span class="hl-0">(</span><span class="hl-4">url</span><span class="hl-0">, </span><span class="hl-4">input</span><span class="hl-0">)</span>
</code></pre>

<a href="#training" id="training" style="color: inherit; text-decoration: none;">
  <h3>Training</h3>
</a>
<p>Want to train your model over the network? Just add an endpoint for a backward pass to <code>network.serve</code>:</p>
<pre><code class="language-javascript"><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">network</span><span class="hl-0">.</span><span class="hl-7">serve</span><span class="hl-0">({</span><br/><span class="hl-0">  </span><span class="hl-7">run_model</span><span class="hl-4">:</span><span class="hl-0"> (</span><span class="hl-4">user</span><span class="hl-0">, </span><span class="hl-4">input</span><span class="hl-0">) </span><span class="hl-3">=&gt;</span><span class="hl-0"> {</span><br/><span class="hl-0">    </span><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-6">out</span><span class="hl-0"> = </span><span class="hl-7">model</span><span class="hl-0">(</span><span class="hl-4">input</span><span class="hl-0">)</span><br/><span class="hl-0">    </span><span class="hl-1">// capture a backward pass</span><br/><span class="hl-0">    </span><span class="hl-4">user</span><span class="hl-0">.</span><span class="hl-7">opt</span><span class="hl-0"> = (</span><span class="hl-4">jacobian</span><span class="hl-0">) </span><span class="hl-3">=&gt;</span><span class="hl-0"> {</span><br/><span class="hl-0">      </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">optim</span><span class="hl-0">.</span><span class="hl-7">sgd</span><span class="hl-0">(</span><span class="hl-4">out</span><span class="hl-0">.</span><span class="hl-7">backward</span><span class="hl-0">(</span><span class="hl-4">jacobian</span><span class="hl-0">), </span><span class="hl-8">1e-3</span><span class="hl-0">)</span><br/><span class="hl-0">    }</span><br/><span class="hl-0">    </span><span class="hl-2">return</span><span class="hl-0"> </span><span class="hl-4">out</span><br/><span class="hl-0">  }</span><br/><span class="hl-0">  </span><span class="hl-4">optimize_model</span><span class="hl-0">: (</span><span class="hl-4">user</span><span class="hl-0">, </span><span class="hl-4">jacobian</span><span class="hl-0">) </span><span class="hl-3">=&gt;</span><span class="hl-0"> {</span><br/><span class="hl-0">    </span><span class="hl-1">// run it when that same user gives us a jacobian</span><br/><span class="hl-0">    </span><span class="hl-4">user</span><span class="hl-0">.</span><span class="hl-7">opt</span><span class="hl-0">(</span><span class="hl-4">jacobian</span><span class="hl-0">)</span><br/><span class="hl-0">  }</span><br/><span class="hl-0">})</span>
</code></pre>
<p>And the client can feed the jacobian with <code>network.tfetch</code>:</p>
<pre><code class="language-javascript"><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-6">url</span><span class="hl-0"> = </span><span class="hl-5">&#39;localhost:3000&#39;</span><br/><span class="hl-2">for</span><span class="hl-0"> (</span><span class="hl-3">let</span><span class="hl-0"> </span><span class="hl-4">i</span><span class="hl-0"> </span><span class="hl-3">of</span><span class="hl-0"> </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-7">range</span><span class="hl-0">(</span><span class="hl-8">100</span><span class="hl-0">)) {</span><br/><span class="hl-0">  </span><span class="hl-3">const</span><span class="hl-0"> [</span><span class="hl-6">input</span><span class="hl-0">, </span><span class="hl-6">ref_output</span><span class="hl-0">] = </span><span class="hl-7">get_data</span><span class="hl-0">()</span><br/><span class="hl-0">  </span><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-6">output</span><span class="hl-0"> = </span><span class="hl-2">await</span><span class="hl-0"> </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">network</span><span class="hl-0">.</span><span class="hl-7">tfetch</span><span class="hl-0">(</span><span class="hl-5">`</span><span class="hl-3">${</span><span class="hl-4">url</span><span class="hl-3">}</span><span class="hl-5">/run_model`</span><span class="hl-0">, </span><span class="hl-4">input</span><span class="hl-0">)</span><br/><br/><span class="hl-0">  </span><span class="hl-1">// get the jacobian from a loss function</span><br/><span class="hl-0">  </span><span class="hl-4">output</span><span class="hl-0">.</span><span class="hl-4">requires_grad</span><span class="hl-0"> = </span><span class="hl-3">true</span><br/><span class="hl-0">  </span><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-6">loss</span><span class="hl-0"> = </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">loss</span><span class="hl-0">.</span><span class="hl-7">mse</span><span class="hl-0">(</span><span class="hl-4">output</span><span class="hl-0">, </span><span class="hl-4">ref_output</span><span class="hl-0">)</span><br/><span class="hl-0">  </span><span class="hl-4">loss</span><span class="hl-0">.</span><span class="hl-7">backward</span><span class="hl-0">()</span><br/><br/><span class="hl-0">  </span><span class="hl-1">// send that jacobian back</span><br/><span class="hl-0">  </span><span class="hl-2">await</span><span class="hl-0"> </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">network</span><span class="hl-0">.</span><span class="hl-7">tfetch</span><span class="hl-0">(</span><span class="hl-5">`</span><span class="hl-3">${</span><span class="hl-4">url</span><span class="hl-3">}</span><span class="hl-5">/optimize_model`</span><span class="hl-0">, </span><span class="hl-4">output</span><span class="hl-0">.</span><span class="hl-4">grad</span><span class="hl-0">)</span><br/><span class="hl-0">}</span>
</code></pre>

<a href="#wrappers" id="wrappers" style="color: inherit; text-decoration: none;">
  <h3>Wrappers</h3>
</a>
<p>Shumai provides wrapper for the above setup code. <a href="../functions/network.serve_model.html"><code>network.serve_model</code></a> will create <code>/forward</code> and <code>/optimize</code> endpoints for you.</p>
<pre><code class="language-javascript"><span class="hl-2">import</span><span class="hl-0"> </span><span class="hl-3">*</span><span class="hl-0"> </span><span class="hl-2">as</span><span class="hl-0"> </span><span class="hl-4">sm</span><span class="hl-0"> </span><span class="hl-2">from</span><span class="hl-0"> </span><span class="hl-5">&#39;@shumai/shumai&#39;</span><br/><span class="hl-2">import</span><span class="hl-0"> { </span><span class="hl-4">model</span><span class="hl-0"> } </span><span class="hl-2">from</span><span class="hl-0"> </span><span class="hl-5">&#39;./model&#39;</span><br/><br/><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">network</span><span class="hl-0">.</span><span class="hl-7">serve_model</span><span class="hl-0">(</span><span class="hl-4">model</span><span class="hl-0">, </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">optim</span><span class="hl-0">.</span><span class="hl-4">sgd</span><span class="hl-0">)</span>
</code></pre>
<p>And the client can attach with <a href="../functions/network.remote_model.html"><code>network.remote_model</code></a>, which attaches a hook to <code>backward</code> for automatic gradients.</p>
<pre><code class="language-javascript"><span class="hl-2">import</span><span class="hl-0"> </span><span class="hl-3">*</span><span class="hl-0"> </span><span class="hl-2">as</span><span class="hl-0"> </span><span class="hl-4">sm</span><span class="hl-0"> </span><span class="hl-2">from</span><span class="hl-0"> </span><span class="hl-5">&#39;@shumai/shumai&#39;</span><br/><br/><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-6">model</span><span class="hl-0"> = </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">network</span><span class="hl-0">.</span><span class="hl-7">remote_model</span><span class="hl-0">(</span><span class="hl-5">&#39;localhost:3000&#39;</span><span class="hl-0">)</span><br/><br/><span class="hl-2">for</span><span class="hl-0"> (</span><span class="hl-3">let</span><span class="hl-0"> </span><span class="hl-4">i</span><span class="hl-0"> </span><span class="hl-3">of</span><span class="hl-0"> </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-7">range</span><span class="hl-0">(</span><span class="hl-8">100</span><span class="hl-0">)) {</span><br/><span class="hl-0">  </span><span class="hl-3">const</span><span class="hl-0"> [</span><span class="hl-6">input</span><span class="hl-0">, </span><span class="hl-6">ref_output</span><span class="hl-0">] = </span><span class="hl-7">get_data</span><span class="hl-0">()</span><br/><span class="hl-0">  </span><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-6">output</span><span class="hl-0"> = </span><span class="hl-2">await</span><span class="hl-0"> </span><span class="hl-7">model</span><span class="hl-0">(</span><span class="hl-4">input</span><span class="hl-0">)</span><br/><br/><span class="hl-0">  </span><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-6">loss</span><span class="hl-0"> = </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">loss</span><span class="hl-0">.</span><span class="hl-7">mse</span><span class="hl-0">(</span><span class="hl-4">ref_output</span><span class="hl-0">, </span><span class="hl-4">output</span><span class="hl-0">)</span><br/><br/><span class="hl-0">  </span><span class="hl-1">// async now, as it propagates through the network</span><br/><span class="hl-0">  </span><span class="hl-2">await</span><span class="hl-0"> </span><span class="hl-4">loss</span><span class="hl-0">.</span><span class="hl-7">backward</span><span class="hl-0">()</span><br/><span class="hl-0">}</span>
</code></pre>

<a href="#composition" id="composition" style="color: inherit; text-decoration: none;">
  <h3>Composition</h3>
</a>
<p>Want to run more than just a trivial remote trainer? Below is a distributed model parallel and pipelined server.  We invoke multiple remote models and then make our own model server.</p>
<pre><code class="language-javascript"><span class="hl-2">import</span><span class="hl-0"> </span><span class="hl-3">*</span><span class="hl-0"> </span><span class="hl-2">as</span><span class="hl-0"> </span><span class="hl-4">sm</span><span class="hl-0"> </span><span class="hl-2">from</span><span class="hl-0"> </span><span class="hl-5">&#39;@shumai/shumai&#39;</span><br/><br/><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-6">A</span><span class="hl-0"> = </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">network</span><span class="hl-0">.</span><span class="hl-7">remote_model</span><span class="hl-0">(</span><span class="hl-5">&#39;localhost:3001&#39;</span><span class="hl-0">)</span><br/><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-6">B</span><span class="hl-0"> = </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">network</span><span class="hl-0">.</span><span class="hl-7">remote_model</span><span class="hl-0">(</span><span class="hl-5">&#39;localhost:3002&#39;</span><span class="hl-0">)</span><br/><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-6">C</span><span class="hl-0"> = </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">network</span><span class="hl-0">.</span><span class="hl-7">remote_model</span><span class="hl-0">(</span><span class="hl-5">&#39;localhost:3003&#39;</span><span class="hl-0">)</span><br/><br/><span class="hl-1">// no need to wrap this, autograd knows what&#39;s up</span><br/><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-6">weight</span><span class="hl-0"> = </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-7">randn</span><span class="hl-0">([</span><span class="hl-8">128</span><span class="hl-0">, </span><span class="hl-8">128</span><span class="hl-0">]).</span><span class="hl-7">requireGrad</span><span class="hl-0">()</span><br/><br/><span class="hl-3">function</span><span class="hl-0"> </span><span class="hl-7">model</span><span class="hl-0">(</span><span class="hl-4">input</span><span class="hl-0">) {</span><br/><span class="hl-0">  </span><span class="hl-1">// this will run in parallel</span><br/><span class="hl-0">  </span><span class="hl-3">const</span><span class="hl-0"> [</span><span class="hl-6">a</span><span class="hl-0">, </span><span class="hl-6">b</span><span class="hl-0">] = </span><span class="hl-2">await</span><span class="hl-0"> </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">util</span><span class="hl-0">.</span><span class="hl-7">all</span><span class="hl-0">(</span><br/><span class="hl-0">    </span><span class="hl-7">A</span><span class="hl-0">(</span><span class="hl-4">input</span><span class="hl-0">),</span><br/><span class="hl-0">    </span><span class="hl-7">B</span><span class="hl-0">(</span><span class="hl-4">input</span><span class="hl-0">)</span><br/><span class="hl-0">  )</span><br/><span class="hl-0">  </span><span class="hl-1">// automatically pipelined (isn&#39;t async great?)</span><br/><span class="hl-0">  </span><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-6">c</span><span class="hl-0"> = </span><span class="hl-2">await</span><span class="hl-0"> </span><span class="hl-7">C</span><span class="hl-0">(</span><span class="hl-4">a</span><span class="hl-0">)</span><br/><span class="hl-0">  </span><span class="hl-2">return</span><span class="hl-0"> </span><span class="hl-4">c</span><span class="hl-0">.</span><span class="hl-7">mul</span><span class="hl-0">(</span><span class="hl-4">b</span><span class="hl-0">).</span><span class="hl-7">matmul</span><span class="hl-0">(</span><span class="hl-4">weight</span><span class="hl-0">)</span><br/><span class="hl-0">}</span><br/><br/><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">network</span><span class="hl-0">.</span><span class="hl-7">serve_model</span><span class="hl-0">(</span><span class="hl-4">model</span><span class="hl-0">, </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">optim</span><span class="hl-0">.</span><span class="hl-4">sgd</span><span class="hl-0">)</span>
</code></pre>
<p>Same client as before :)</p>

<a href="#what-about-debugging" id="what-about-debugging" style="color: inherit; text-decoration: none;">
  <h3>What about debugging?</h3>
</a>
<p>All <code>network.serve*</code> methods automatically give us basic <code>/statistics</code> as JSON:</p>
<pre><code class="language-bash"><span class="hl-0">$ curl -s localhost:3000/statistics | jq</span><br/><span class="hl-0">(env) bwasti@bwasti-mbp shumai % curl -s localhost:3000/statistics|jq</span><br/><span class="hl-0">{</span><br/><span class="hl-0">  </span><span class="hl-5">&quot;forward&quot;</span><span class="hl-0">: {</span><br/><span class="hl-0">    </span><span class="hl-5">&quot;hits&quot;</span><span class="hl-0">: 1000,</span><br/><span class="hl-0">    </span><span class="hl-5">&quot;seconds&quot;</span><span class="hl-0">: 0.12337932200005891</span><br/><span class="hl-0">  },</span><br/><span class="hl-0">  </span><span class="hl-5">&quot;optimize&quot;</span><span class="hl-0">: {</span><br/><span class="hl-0">    </span><span class="hl-5">&quot;hits&quot;</span><span class="hl-0">: 1000,</span><br/><span class="hl-0">    </span><span class="hl-5">&quot;seconds&quot;</span><span class="hl-0">: 0.16975503499999103</span><br/><span class="hl-0">  }</span><br/><span class="hl-0">}</span>
</code></pre>
<p>but we can always add more:</p>
<pre><code class="language-javascript"><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">network</span><span class="hl-0">.</span><span class="hl-7">serve_model</span><span class="hl-0">(</span><span class="hl-4">model</span><span class="hl-0">, </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">optim</span><span class="hl-0">.</span><span class="hl-4">sgd</span><span class="hl-0">, {</span><span class="hl-4">port:</span><span class="hl-8">3000</span><span class="hl-0">}, {</span><br/><span class="hl-0">  </span><span class="hl-7">statistics</span><span class="hl-4">:</span><span class="hl-0"> () </span><span class="hl-3">=&gt;</span><span class="hl-0"> {</span><br/><span class="hl-0">    </span><span class="hl-2">return</span><span class="hl-0"> {</span><span class="hl-4">weight:</span><span class="hl-0"> </span><span class="hl-4">weight</span><span class="hl-0">.</span><span class="hl-7">mean</span><span class="hl-0">().</span><span class="hl-7">toFloat32</span><span class="hl-0">()}</span><br/><span class="hl-0">  }</span><br/><span class="hl-0">})</span>
</code></pre>
<pre><code class="language-bash"><span class="hl-0">$ curl -s localhost:3000/statistics | jq .weight</span><br/><span class="hl-0">0.1128062978386879</span>
</code></pre>
<p>including recursively:</p>
<pre><code class="language-javascript"><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">network</span><span class="hl-0">.</span><span class="hl-7">serve_model</span><span class="hl-0">(</span><span class="hl-4">model</span><span class="hl-0">, </span><span class="hl-4">sm</span><span class="hl-0">.</span><span class="hl-4">optim</span><span class="hl-0">.</span><span class="hl-4">sgd</span><span class="hl-0">, {</span><span class="hl-4">port:</span><span class="hl-8">3000</span><span class="hl-0">}, {</span><br/><span class="hl-0">  </span><span class="hl-7">statistics</span><span class="hl-4">:</span><span class="hl-0"> </span><span class="hl-3">async</span><span class="hl-0"> () </span><span class="hl-3">=&gt;</span><span class="hl-0"> {</span><br/><span class="hl-0">    </span><span class="hl-2">return</span><span class="hl-0"> {</span><span class="hl-4">A:</span><span class="hl-0"> </span><span class="hl-2">await</span><span class="hl-0"> (</span><span class="hl-2">await</span><span class="hl-0"> </span><span class="hl-7">fetch</span><span class="hl-0">(</span><span class="hl-5">&#39;localhost:3001/statistics&#39;</span><span class="hl-0">)).</span><span class="hl-7">json</span><span class="hl-0">()}</span><br/><span class="hl-0">  }</span><br/><span class="hl-0">})</span>
</code></pre>
</div></section><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/facebookresearch/shumai/blob/9158fba/shumai/network/index.ts#L170">shumai/network/index.ts:170</a></li></ul></aside>
<section class="tsd-panel-group tsd-index-group">
<section class="tsd-panel tsd-index-panel">
<h3 class="tsd-index-heading uppercase">Index</h3>
<section class="tsd-index-section">
<h3 class="tsd-index-heading">Type Aliases</h3>
<div class="tsd-index-list"><a href="../types/network.OpStats.html" class="tsd-index-link tsd-kind-type-alias tsd-parent-kind-namespace"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><rect fill="var(--color-icon-background)" stroke="var(--color-ts)" stroke-width="1.5" x="1" y="1" width="22" height="22" rx="6" id="icon-4194304-path"></rect><path d="M11.31 16V8.224H8.91V7.24H14.79V8.224H12.39V16H11.31Z" fill="var(--color-text)" id="icon-4194304-text"></path></svg><span>Op<wbr/>Stats</span></a>
<a href="../types/network.RouteStats.html" class="tsd-index-link tsd-kind-type-alias tsd-parent-kind-namespace"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-4194304-path"></use><use href="#icon-4194304-text"></use></svg><span>Route<wbr/>Stats</span></a>
<a href="../types/network.TensorOpStats.html" class="tsd-index-link tsd-kind-type-alias tsd-parent-kind-namespace"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-4194304-path"></use><use href="#icon-4194304-text"></use></svg><span>Tensor<wbr/>Op<wbr/>Stats</span></a>
</div></section>
<section class="tsd-index-section">
<h3 class="tsd-index-heading">Functions</h3>
<div class="tsd-index-list"><a href="../functions/network.backoff.html" class="tsd-index-link tsd-kind-function tsd-parent-kind-namespace tsd-is-private"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><rect fill="var(--color-icon-background)" stroke="var(--color-ts-function)" stroke-width="1.5" x="1" y="1" width="22" height="22" rx="6" id="icon-64-path"></rect><path d="M9.39 16V7.24H14.55V8.224H10.446V11.128H14.238V12.112H10.47V16H9.39Z" fill="var(--color-text)" id="icon-64-text"></path></svg><span>backoff</span></a>
<a href="../functions/network.decode.html" class="tsd-index-link tsd-kind-function tsd-parent-kind-namespace tsd-is-private"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg><span>decode</span></a>
<a href="../functions/network.decodeBuffer.html" class="tsd-index-link tsd-kind-function tsd-parent-kind-namespace tsd-is-private"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg><span>decode<wbr/>Buffer</span></a>
<a href="../functions/network.encode.html" class="tsd-index-link tsd-kind-function tsd-parent-kind-namespace tsd-is-private"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg><span>encode</span></a>
<a href="../functions/network.remote_model.html" class="tsd-index-link tsd-kind-function tsd-parent-kind-namespace"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg><span>remote_<wbr/>model</span></a>
<a href="../functions/network.remote_runner.html" class="tsd-index-link tsd-kind-function tsd-parent-kind-namespace"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg><span>remote_<wbr/>runner</span></a>
<a href="../functions/network.serve.html" class="tsd-index-link tsd-kind-function tsd-parent-kind-namespace"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg><span>serve</span></a>
<a href="../functions/network.serve_model.html" class="tsd-index-link tsd-kind-function tsd-parent-kind-namespace"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg><span>serve_<wbr/>model</span></a>
<a href="../functions/network.serve_runner.html" class="tsd-index-link tsd-kind-function tsd-parent-kind-namespace"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg><span>serve_<wbr/>runner</span></a>
<a href="../functions/network.tfetch.html" class="tsd-index-link tsd-kind-function tsd-parent-kind-namespace"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg><span>tfetch</span></a>
</div></section></section></section></div>
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
<div class="tsd-navigation settings">
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
<h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><path d="M4.93896 8.531L12 15.591L19.061 8.531L16.939 6.409L12 11.349L7.06098 6.409L4.93896 8.531Z" fill="var(--color-text)"></path></svg> Settings</h3></summary>
<div class="tsd-accordion-details">
<div class="tsd-filter-visibility">
<h4 class="uppercase">Member Visibility</h4><form>
<ul id="tsd-filter-options">
<li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li>
<li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-private" name="private"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Private</span></label></li>
<li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li>
<li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></form></div>
<div class="tsd-theme-toggle">
<h4 class="uppercase">Theme</h4><select id="theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div>
<nav class="tsd-navigation primary">
<details class="tsd-index-accordion" open><summary class="tsd-accordion-summary">
<h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><path d="M4.93896 8.531L12 15.591L19.061 8.531L16.939 6.409L12 11.349L7.06098 6.409L4.93896 8.531Z" fill="var(--color-text)"></path></svg> Modules</h3></summary>
<div class="tsd-accordion-details">
<ul>
<li class="current"><a href="../modules.html">@shumai/shumai</a>
<ul>
<li class="tsd-kind-namespace"><a href="io.html">io</a></li>
<li class="tsd-kind-namespace"><a href="loss.html">loss</a></li>
<li class="tsd-kind-namespace"><a href="module.html">module</a></li>
<li class="current selected tsd-kind-namespace"><a href="network.html">network</a></li>
<li class="tsd-kind-namespace"><a href="optim.html">optim</a></li>
<li class="tsd-kind-namespace"><a href="util.html">util</a></li></ul></li></ul></div></details></nav>
<nav class="tsd-navigation secondary menu-sticky">
<ul>
<li class="tsd-kind-type-alias tsd-parent-kind-namespace"><a href="../types/network.OpStats.html" class="tsd-index-link"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-4194304-path"></use><use href="#icon-4194304-text"></use></svg>Op<wbr/>Stats</a></li>
<li class="tsd-kind-type-alias tsd-parent-kind-namespace"><a href="../types/network.RouteStats.html" class="tsd-index-link"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-4194304-path"></use><use href="#icon-4194304-text"></use></svg>Route<wbr/>Stats</a></li>
<li class="tsd-kind-type-alias tsd-parent-kind-namespace"><a href="../types/network.TensorOpStats.html" class="tsd-index-link"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-4194304-path"></use><use href="#icon-4194304-text"></use></svg>Tensor<wbr/>Op<wbr/>Stats</a></li>
<li class="tsd-kind-function tsd-parent-kind-namespace tsd-is-private"><a href="../functions/network.backoff.html" class="tsd-index-link"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg>backoff</a></li>
<li class="tsd-kind-function tsd-parent-kind-namespace tsd-is-private"><a href="../functions/network.decode.html" class="tsd-index-link"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg>decode</a></li>
<li class="tsd-kind-function tsd-parent-kind-namespace tsd-is-private"><a href="../functions/network.decodeBuffer.html" class="tsd-index-link"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg>decode<wbr/>Buffer</a></li>
<li class="tsd-kind-function tsd-parent-kind-namespace tsd-is-private"><a href="../functions/network.encode.html" class="tsd-index-link"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg>encode</a></li>
<li class="tsd-kind-function tsd-parent-kind-namespace"><a href="../functions/network.remote_model.html" class="tsd-index-link"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg>remote_<wbr/>model</a></li>
<li class="tsd-kind-function tsd-parent-kind-namespace"><a href="../functions/network.remote_runner.html" class="tsd-index-link"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg>remote_<wbr/>runner</a></li>
<li class="tsd-kind-function tsd-parent-kind-namespace"><a href="../functions/network.serve.html" class="tsd-index-link"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg>serve</a></li>
<li class="tsd-kind-function tsd-parent-kind-namespace"><a href="../functions/network.serve_model.html" class="tsd-index-link"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg>serve_<wbr/>model</a></li>
<li class="tsd-kind-function tsd-parent-kind-namespace"><a href="../functions/network.serve_runner.html" class="tsd-index-link"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg>serve_<wbr/>runner</a></li>
<li class="tsd-kind-function tsd-parent-kind-namespace"><a href="../functions/network.tfetch.html" class="tsd-index-link"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-64-path"></use><use href="#icon-64-text"></use></svg>tfetch</a></li></ul></nav></div></div>
<div class="container tsd-generator">
<p>Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></div>
<div class="overlay"></div><script src="../assets/main.js"></script></body></html>